package com.atguigu.lease.admin.controller;

import com.atguigu.lease.admin.service.FacilityInfoService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.bean.FacilityInfo;
import com.atguigu.lease.model.enums.ItemTypeEnum;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name="配套管理模块")
@RestController
@RequestMapping("/admin/facility")
public class FacilityController {
    @Autowired
    FacilityInfoService facilityInfoService;
    @Operation(summary = "1、根据Type类型查询配套列表")
    @GetMapping("/list")
    public Result<List<FacilityInfo>> list(
            @Parameter(in = ParameterIn.QUERY , description = "配套类型：1-公寓,2-房间")
            @RequestParam(required = false,value = "type") ItemTypeEnum type  //接受参数：springmvc消息转换器生效
    ){
        //响应的结果转为json响应给前端时， @JsonValue注解标注的属性会 作为FacilityInfo对象 type属性的值响应
        // data = [{ id:1 , name:xx , type: code值 }]
        return Result.ok(facilityInfoService.list(Wrappers
                        .lambdaQuery(FacilityInfo.class)
                        //type不为空，mp根据 在枚举类属性上标注的@EnumValue注解获取属性值传参
                        //mp查询到表的结果时， 自动解析type字段的值 给枚举类使用了@EnumValue注解的属性
                        .eq(type!=null , FacilityInfo::getType , type)
                )
        );
    }
    //新增或更新
    @Operation(summary = "2、新增或更新配套")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody FacilityInfo facilityInfo){
        return Result.ok(facilityInfoService.saveOrUpdate(facilityInfo));
    }
    //删除
    @Operation(summary = "3、根据id删除配套")
    @DeleteMapping("/deleteById")
    public Result deleteById(
            @Parameter(in = ParameterIn.QUERY , description = "主键值")
            @RequestParam Integer id){
        return Result.ok(facilityInfoService.removeById(id));
    }
}
